package com.yan_jiu_sheng.LeetCodeHot100.AC;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * https://leetcode.cn/problems/binary-tree-inorder-traversal/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 * <p>
 * 莫里斯解法：
 * https://leetcode.cn/problems/binary-tree-inorder-traversal/solutions/96765/dong-hua-yan-shi-94-er-cha-shu-de-zhong-xu-bian-li/?envType=study-plan-v2&envId=top-100-liked
 *
 * @author yulongTian
 * @create 2024-06-01 13:31
 */
public class Test36 {
    public static void main(String[] args) {

    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        preTravel(root, res);
        return res;
    }

    private void preTravel(TreeNode root, List<Integer> res) {
        if (root == null) {
            return;
        }
        if (root.left != null) {
//            res.add(root.left.val);
            preTravel(root.left, res);
        }
        res.add(root.val);
        if (root.right != null) {
//            res.add(root.right.val);
            preTravel(root.right, res);
        }
    }

}
